var toolbar;
var tools = [];
var totals = 100;
var catalogId = -1;
var optInit = {
	"callback" : pageselectCallback,
	"items_per_page" : 10,
	"num_display_entries" : 10,
	"num_edge_entries" : 2,
	"prev_text" : "上页",
	"next_text" : "下页",
	"link_to" : "javascript:;"

};
$(function() {
	
	$(document).ajaxStart(function() {
		$.blockUI({
			"message" : "正在执行请求的操作,请稍候... ..."
		});
	});
	$(document).ajaxSuccess(function() {
		$.unblockUI();
	});
	$(document).ajaxStop(function() {
		$.unblockUI();
	});
	$(document).ajaxError(function() {
		$.unblockUI();
	});

	initToolbars();
});
function logout() {
	window.open(logouturl, '_self');
}
function initToolbars() {

	$.ajax({
		"url" : "newscatalog/news-catalog!catalog?bean.sid=" + parentId,
		"success" : function(data) {
			
			if (data.length == 0)
				return;
			for ( var i = 0; i < data.length; i++) {

				var d = data[i];

				var t = {
					did : d['sid'],
					extlink : d['external'],
					link : d['href'],
					type : 'button',
					text : d['name'],
					bodyStyle : 'new',
					useable : 'T',
					handler : function() {							
						initPagger(this.did, this.extlink, this.link);
					}
				};

				tools.push(t);

			}
			toolbar = new Toolbar({
				renderTo : 'mytoolbar',
				// border: 'top',
				items : tools

			});
			toolbar.render();
			// 初始加载第一个分类的第一页数据
			initPagger(data[0]['sid'],'false','');
		}
	});

}
function initPagger(catalog, extlink, href) {
	
	if (extlink=='false') {//如果是内部需要授权的url
	   
		catalogId = catalog;
		$.getJSON("newsinfo/news-info!rowsbycatalog?catalog=" + catalog,
				function(data) {

					totals = data['count'];
					$("#pagination").pagination(totals, optInit);
					loadContent(catalog, 0);
				});
	}else{	
		loadExtlink(href);
	}

}
function pageselectCallback(page_index, jq) {
	loadContent(catalogId, page_index);
	return false;
}

/**
 * 加载当前分类
 * 
 * @param page
 *            页码
 * 
 */
function loadContent(catalog, page) {
	// alert(catalogId);
	$.ajax({
		"url" : "newsinfo/news-info!load",
		"data" : "catalog=" + catalog + "&page=" + page,
		"success" : function(data) {
			
			fillTable(data);
		}

	});
}

function loadExtlink(link){
	fillExtTable(link);
}
function fillExtTable(link){
	$("#newlist").hide();
	$("#extlink").show();
	var datacon = $("#dataList");
	datacon.empty();
	var tr = $("<tr></tr>");
	datacon.append(tr);
	var md  =$("<td></td>");
	tr.append(md);
	//alert(link);
	

    //link='http://www.qinghua.com/main_main.asp?id=25&disTitle=0';
	$("#extlink").attr("src",link);
	
}


function fillTable(data) {
	$("#newlist").show();
	$("#extlink").hide();
	var datacon = $("#dataList");
	datacon.empty();
	// return;
	var cstr = '';

	for ( var j = 0; j < data.length; j++) {
		var d = data[j];

		cstr += "<tr><td><img src='" + sbase + "images/li-img.png'/></td>";
		cstr += "<td><a href='newsinfo/news-info!show?bean.sid=" + d['sid']
				+ "' target='_blank'>" + d['name'] + "</a></td>";
		// cstr+="<td>"+d['catalog']['name']+"</td>";
		cstr += "<td>" + d['date'] + "</td></tr>";
		// datacon.append(cstr);
	}
	datacon.append(cstr);
}